from django.shortcuts import render
from rest_framework.views import APIView
from rest_framework.response import Response

from rest_framework import status
# Create your views here.
from .models import Tables, Dishes_Taste
from .serializer import TatlrSer


# 添加口味
class TablesAdd(APIView):
    def post(self, request):
        name = request.data.get('name')
        tsasd = Tables.objects.filter(name=name)
        if tsasd.count() > 0:
            tasd = Tables.objects.create(name=name)
            tasd.save()
            return Response({'messages': ' Taste added successfully', 'code': status.HTTP_201_CREATED})
        else:
            return Response({'messages': 'Error adding, please retype', 'code': status.HTTP_400_BAD_REQUEST})


#口味具体口味添加
class TaseApi(APIView):
    def poot(self, request):
        gid = request.data.get('gid')
        name = request.data.get('name')
        taser = Tables.objects.filter(id=gid).all()
        if taser.count() < 0:
            return Response({'messages': 'Please add flavor', 'code': status.HTTP_400_BAD_REQUEST})

        taste = Dishes_Taste.objects.create(taste=taser, dishes=taser, desc_name=name, )
        taste.save()
        return Response({'messages': 'Flavor details added successfully', 'code': status.HTTP_200_OK})


# 展示口味
class TastesShow(APIView):
    def post(self, request):
        taser = Dishes_Taste.objects.all()
        taste = TatlrSer(taser, many=True)
        return Response({taste.data})
